package com.lingshi.shopping.exception;

import cn.dev33.satoken.exception.NotPermissionException;
import com.lingshi.shopping.enums.ErrorEnums;
import com.lingshi.shopping.result.BaseResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理业务逻辑异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusException.class)
    public BaseResult handleBusException(BusException e) {
        Integer code = e.getCode();
        String msg = e.getMsg();
        return BaseResult.error(code,msg);
    }

    /**
     * 处理没有权限异常
     * @param e
     * @return
     */
    @ExceptionHandler(NotPermissionException.class)
    public BaseResult handleNotPermissionException(NotPermissionException e) {
        Integer code = e.getCode();
        String msg = e.getMessage();
        return BaseResult.error(code,msg);
    }

    @ExceptionHandler(Exception.class)
    public BaseResult handleException(Exception e) {
//        e.printStackTrace();
        return BaseResult.error(ErrorEnums.SYSTEM_ERROR.getCode(), ErrorEnums.SYSTEM_ERROR.getMessage());
    }
}
